package com.example.tourstor;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.leon.lib.settingview.LSettingItem;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.xml.transform.Result;

public class MakePlanActivity extends AppCompatActivity {
    private static final String TAG = "MakePlanActivity";
    private TextView title;
    private com.leon.lib.settingview.LSettingItem item_time,item_destin,item_cost;
    private Button title_btn_left,title_btn_right;


    private Connection con ;
    private PreparedStatement ps=null,ps2;//操作整合sql语句的对象
    DBOpenHelper dbOpenHelper;

    private int ttime,tcost;
    private Thread thread;
    private java.sql.Date date1;
    private String name,wtime,mtime,date,tdestin,click;
    private float wmoney,mmoney;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_make_plan);
        setTitle();

        item_time=findViewById(R.id.item_time);
        item_destin=findViewById(R.id.item_destin);
        item_cost=findViewById(R.id.item_cost);


        final SharedPreferences sharedPreferences=getSharedPreferences("Cname", Activity.MODE_PRIVATE);
        //name=sharedPreferences.getString("Cname",null);
        name="41623060";
        Log.i(TAG, "MyPlanActivity2: 登录名是："+name);

        if(name!=null){
            item_time.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
                @Override
                public void click() {
                    Intent intent=new Intent(MakePlanActivity.this,TravelTimeActivity.class);
                    intent.putExtra("Cname",name);
                    startActivityForResult(intent,1);
                    Log.i(TAG, "click: 跳转到选择时间界面：");
                }
            });
            item_destin.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
                @Override
                public void click() {
                    Intent intent=new Intent(MakePlanActivity.this,TravelDestinActivity.class);
                    startActivityForResult(intent,2);
                    Log.i(TAG, "click: 跳转到选择地点界面：");
                }
            });
            item_cost.setmOnLSettingItemClick(new LSettingItem.OnLSettingItemClick() {
                @Override
                public void click() {
                    Intent intent=new Intent(MakePlanActivity.this,TravelCostActivity.class);
                    startActivityForResult(intent,3);
                    Log.i(TAG, "click: 跳转到储蓄目标界面：");
                }
            });
        }

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if((requestCode==1)&&(resultCode==11)){
            date=data.getStringExtra("Tstart");
            Log.i(TAG, "onActivityResult: "+date);
            date1= Date.valueOf(date);
        }else if((requestCode==2)&&(resultCode==22)){
            tdestin=data.getStringExtra("Tdestin");

        }else if((requestCode==3)&&(resultCode==33)){
            click=data.getStringExtra("click");
            if(click.equals("0")){
                wmoney=data.getFloatExtra("WMm",0.0f);
                wtime=data.getStringExtra("WMt");
            }else if(click.equals("1")){
                mmoney=data.getFloatExtra("WMm",0.0f);
                mtime=data.getStringExtra("WMt");
            }

            tcost=data.getIntExtra("Tcost",0);
            ttime=data.getIntExtra("Ttime",0);
        }

        Intent();

    }

    public void Intent(){

        title_btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(date==null){
                    Toast.makeText(MakePlanActivity.this, "请选择出行时间", Toast.LENGTH_LONG).show();
                }else if(tdestin==null){
                    Toast.makeText(MakePlanActivity.this, "请选择出行地点", Toast.LENGTH_LONG).show();
                }else if(tcost==0){
                    Toast.makeText(MakePlanActivity.this, "请确定储蓄目标", Toast.LENGTH_LONG).show();
                }else {
                    thread=new Thread(new Runnable() {
                        @Override
                        public void run() {

                            Handler handler = new Handler(Looper.getMainLooper());
                            con = dbOpenHelper.getConn();
                            String sql1="";
                            if(click.equals("0")){
                                sql1=",Wtime= '"+wtime+"',Wmoney= "+wmoney;
                                Log.i(TAG, "run: 实现方式："+wtime+wmoney);

                            }else if(click.equals("1")){
                                sql1=",Mtime= '"+mtime+"',Mmoney= "+mmoney;
                                Log.i(TAG, "run: 实现方式："+mtime+mmoney);

                            }
                            Log.i(TAG, "run: 储蓄目标以及实现时间："+tcost+" "+ttime);
                            String sql2 = "update makeplan set Tcost= "+tcost+",Ttime= "+ttime+sql1+" where datediff ( Tstart , '"+date1+"' ) = 0";
                            String sql = "insert into makeplan(Uid,Tstart,Tdestin) values(?,?,?)";

                            try {
                                ps = con.prepareStatement(sql);
                                ps.setString(1,name);
                                ps.setDate(2, date1);
                                ps.setString(3,tdestin);
                                ps.executeUpdate();
                                Log.i(TAG, "init: 插入成功");

                                ps2 = con.prepareStatement(sql2);
                                ps2.executeUpdate();
                                Log.i(TAG, "init: 更新成功");

                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MakePlanActivity.this, "计划创建成功", Toast.LENGTH_LONG).show();
                                    }
                                });

                                con.close();//一定要关闭
                                ps.close();
                                ps2.close();
                            }
                            catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    thread.start();
                }


                finish();
            }
        });

    }
    public void setTitle(){
        title = findViewById(R.id.title_text);
        title.setText("制定计划");
        title_btn_left = findViewById(R.id.title_btn_left);
        title_btn_right=findViewById(R.id.title_btn_right);
        title_btn_left.setText("");
        title_btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
    }
}
